package com.trnnn.strutslit;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.StringTokenizer;

import javax.servlet.FilterConfig;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.trnnn.strutslit.ActionTable.ActionModel;

public class ActionController {
	private FilterConfig config;

	public ActionController(FilterConfig config) {
		this.config = config;
		initConfig();
	}

	private void initConfig() {
		config.getInitParameterNames();
	}

	public void doAction(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		SessionContext context = SessionContext.newInstance(request, response);
		this.doAction(context);
	}

	private void doAction(SessionContext context) {
		String requestUri = context.getRequest().getRequestURI();
		String contextPath = context.getRequest().getContextPath();
		String queryString = context.getRequest().getQueryString();
		Debug.output("[ActionController] doAction requestUri ->> " + requestUri);
		Debug.output("[ActionController] doAction contextPath ->> "
				+ contextPath);
		if (contextPath.length() > 0) {
			requestUri = requestUri.replaceFirst(contextPath, "");
		}
		Debug.output("[ActionController] doAction requestUri after ->> "
				+ requestUri);
		Debug.output("[ActionController] doAction queryString after ->> "
				+ queryString);
		Debug.output("-->>" + context.getRequest().getParameter("p1"));
		Set<String> keyset = context.getRequest().getParameterMap().keySet();
		for (String key : keyset) {
			String[] values = context.getRequest().getParameterMap().get(key);
			for (String value : values) {
				Debug.output(key + "->>" + value);
			}
		}

		ActionModel actionModel=matchUri(requestUri, context);
		if(actionModel==null){
			//TODO: handler 404 here
		}
		
		
	}

	private ActionModel matchUri(String requestUri, SessionContext context) {
		// reuqest uri ->>"/abc"
		List<String> strs=parseUri(requestUri);
		Set<ActionKey> actionKeys = ActionTable.getActionKeySet();
		ActionKey actionKey=findKey(strs,actionKeys);
		ActionModel actionModel=ActionTable.getActionModel(actionKey);
		return actionModel;
	}
	
	private ActionKey findKey(List<String> strs,Set<ActionKey> actionKeys){
		for (ActionKey actionKey : actionKeys) {
			if(matchActionKey(actionKey,strs))
				return actionKey;
		}
		return null;
	}

	private boolean matchActionKey(ActionKey actionKey,List<String> urlSlips){
		// String actionName = actionKey.getActionName();
		// String namespace = actionKey.getNamespace();
		// String pattern = actionKey.getPattern();
		// int order = actionKey.getOrder();
		
		return false;
	}
	
	private List<String> parseUri(String uri) {
		StringTokenizer st = new StringTokenizer(uri, "/", false);
		List<String> strs=new ArrayList<String>();
		while (st.hasMoreTokens()) {
			String s = st.nextToken();
			//System.out.println(s);
			strs.add(s);
		}
		return strs;
	}
}




